noOrders();

//冒泡事件
var delliele;

document.querySelector(".tab-content").onclick = function(event){
    var ele = event.target;

    //“删除”
    if(ele.nodeName == "BUTTON" && ele.className == "del"){
       delliele = ele.parentElement.parentElement;
    }

    //“确认删除”
    else if(ele.nodeName == "BUTTON" && ele.className == "yes"){
        $("#delModal").modal("hide");
        $("#cancelModal").modal("hide");

        //1.后端修改订单状态
        var oNum = delliele.firstElementChild.children[2].innerText;
        $.ajax({
            type:"post",
            url:"updateStatus",
            data:{
                orderNum:oNum
            },
            success:function (result){
                console.log("是否修改成功：" + result);
            }
        })

        //2.删除li
        delliele.remove();
        noOrders();
     }

    else if(ele.nodeName == "BUTTON" && ele.className == "pay"){
        var price = ele.parentElement.previousElementSibling.previousElementSibling.children[1].innerText;
        console.log("price:" + price)
        var num = ele.parentElement.parentElement.firstElementChild.children[2].innerText;
        console.log("num:" + num)
        var addressId = ele.parentElement.previousElementSibling.children[1].innerText;
        console.log("addressId:" + addressId)

        window.location.href = "zhifu" + "/" +  num + "/" + price + "/" + addressId;
    }
    else if (ele.nodeName == "BUTTON" && ele.className == "review"){
        var proId = ele.parentElement.previousElementSibling.children[2].innerText;
        console.log("proId:" + proId)

        window.location.href = "http://localhost:8081/demo/tofabiao/"+proId;

    }

}

//无订单提醒
function noOrders(){
    var eleArr = document.querySelectorAll(".myul");
    for(var i = 0; i < eleArr.length; i++){
          if(eleArr[i].innerText == '' || (eleArr[i].childNodes[0].nodeName == '#text' && eleArr[i].childNodes.length == 1))
          eleArr[i].innerHTML = '<div class="noOrder"><p class="words">没有订单哦~<br>快去首页逛逛吧！</p></div>';
        }
}

//不同状态，操作不同
$(document).ready(function (){
    var arr = [];
    $(".staue").each(function (){
        var con = $(this);
        arr.push(con);
    });
    arr.forEach(function (val){
        if(val.text() == "已完成")
            val.parent().next().html("<button class=\"review\">去评价</button>\n" +
                "                                        <button class=\"del\" data-toggle=\"modal\" data-target=\"#delModal\">删除</button>");
        else if(val.text() == "待付款")
            val.parent().next().html("<button class=\"pay\">立即付款</button>\n" +
                 "                                    <button class=\"del\" data-toggle=\"modal\" data-target=\"#cancelModal\">取消订单</button>");
        else if(val.text() == "已取消")
            val.parent().next().html("<button class=\"del\" data-toggle=\"modal\" data-target=\"#delModal\">删除</button>");
    })

})